/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2021-2025. All rights reserved.
 */

import { Component } from '@ohos/sdkmanager-common';
import path from 'path';

import { ArkComponent } from '../superior/ark-component.js';
import { SdkComponent } from '../superior/sdk-component.js';

import { EtsArkComponent } from './ets-ark-component.js';
import { EtsComponentConst } from "../../const/sdk-const.js";
import fse from 'fs-extra';

/**
 * sdk ets实例
 *
 * @since 2021-12-16
 */
export class SdkEtsComponent extends SdkComponent {
  private readonly _etsLoaderPath: string;
  private readonly _etsV2Path: string;
  private readonly _etsV2UiPluginsLibPath: string;
  private readonly _arkUIPluginPath: string;
  private readonly _arkUIMemoPluginPath: string;
  private readonly _arkuiInteropPluginPath: string;
  private readonly _runEs2AbcStandalone: string;
  private readonly _arkComponent: ArkComponent;
  private readonly _sysCapFilePath: string;
  private readonly _runDeclgenStandalone: string;
  private readonly _etsBaseDir: string;

  constructor(component: Component) {
    super(component);
    this._etsBaseDir = this.getEtsBaseDir();

    this._etsLoaderPath = path.resolve(
      this.getBaseDir(),
      EtsComponentConst.ETS1_1,
      EtsComponentConst.BUILD_TOOLS,
      EtsComponentConst.ETS_LOADER
    );

    this._etsV2Path = path.resolve(
      this.getBaseDir(),
      EtsComponentConst.ETS1_2
    );
    this._etsV2UiPluginsLibPath = path.resolve(
      this._etsV2Path,
      EtsComponentConst.BUILD_TOOLS,
      EtsComponentConst.UI_PLUGINS,
      EtsComponentConst.LIB
    );

    // ArkTS1.2 UI转换插件路径
    this._arkUIPluginPath = path.resolve(
      this._etsV2UiPluginsLibPath,
      EtsComponentConst.UI_PLUGINS,
      EtsComponentConst.INDEX_JS
    );

    // 混合声明生成生成文件ArkUI插件
    this._arkuiInteropPluginPath = path.resolve(this.getBaseDir(), 'static', 'build-tools', 'ui-plugins', 'lib', 'interop-plugins', 'index.js');

    // ArkUI MEMO插件路径
    this._arkUIMemoPluginPath = path.resolve(
      this._etsV2UiPluginsLibPath,
      EtsComponentConst.MEMO_PLUGINS,
      EtsComponentConst.INDEX_JS
    );

    this._runEs2AbcStandalone = path.resolve(
      this._etsLoaderPath,
      EtsComponentConst.LIB,
      EtsComponentConst.FAST_BUILD,
      EtsComponentConst.ARK_COMPILER,
      EtsComponentConst.RUN_ES2ABC_STANDALONE_JS
    );

    this._runDeclgenStandalone = path.resolve(
      this._etsLoaderPath,
      EtsComponentConst.LIB,
      EtsComponentConst.FAST_BUILD,
      EtsComponentConst.ARK_COMPILER,
      EtsComponentConst.INTEROP,
      EtsComponentConst.RUN_DECLGEN_STANDALONE_JS
    );

    this._arkComponent = new EtsArkComponent(this._etsLoaderPath);
    this._sysCapFilePath = path.resolve(this._etsBaseDir, EtsComponentConst.API, EtsComponentConst.DEVICE_DEFINE);
  }

  getEtsBaseDir(): string {
    let etsPath = path.resolve(
      this.getBaseDir(),
      EtsComponentConst.ETS1_1
    );

    if (fse.existsSync(etsPath)) {
      return etsPath;
    }

    etsPath = path.resolve(this.getBaseDir(), EtsComponentConst.ETS);
    if (fse.existsSync(etsPath)) {
      return etsPath;
    }
    return path.resolve(
      this.getBaseDir(),
      EtsComponentConst.ETS1_1
    );
  }

  getEtsLoaderPath(): string {
    return this._etsLoaderPath;
  }

  getBuildSystemPath(): string {
    return this._etsV2Path;
  }

  getOpenHarmonyEtsV2ApiPaths(): string[] {
    return [
      path.resolve(this._etsV2Path, EtsComponentConst.API),
      path.resolve(this._etsV2Path, EtsComponentConst.KITS),
      path.resolve(this._etsV2Path, EtsComponentConst.ARKTS)
    ];
  }

  getArkUIPLuginPath(): string {
    return this._arkUIPluginPath;
  }

  getArkUIMemoPluginPath(): string {
    return this._arkUIMemoPluginPath;
  }

  getArkUIInteropPluginPath(): string {
    return this._arkuiInteropPluginPath;
  }

  getRunEs2AbcStandalone(): string {
    return this._runEs2AbcStandalone;
  }

  getRunDeclgenStandalone(): string {
    return this._runDeclgenStandalone;
  }

  getArkVersion(): string {
    return this._arkComponent.getArkVersion();
  }

  getCompatibleArkVersion(compatibleSdkVersion: number, compatibleSdkVersionStage?: string): string {
    return this._arkComponent.getArkVersion(compatibleSdkVersion, compatibleSdkVersionStage);
  }

  getSysCapFilePath(): string {
    return this._sysCapFilePath;
  }
}
